// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.services;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 * 针对hibernate的一些常用操作类
 * 
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 28 $
 * @since 0.0.1
 */
public class HibernateUtils {
	
	/**
	 * 执行指定的hql,返回操作的记录数
	 * @param session
	 * @param hql
	 * @param parameters
	 * 
	 * @return 一个int值,标识受hql影响的记录数 
	 */
	public static int executeUpdate(HibernateTemplate template, String hql,
			Object... parameters) {
		return  template.bulkUpdate(hql,parameters);
	}
	
	/**
	 * 指定指定的hql，并返回操作的记录数
	 * @param session
	 * @param hql
	 * @param parameters
	 * @return {@link List}类型，包含hql查询的结果
	 */
	public static List executeQuery(HibernateTemplate template, String hql,
			Object... parameters){
		return template.find(hql,parameters);
	}
	
	/**
	 * 创建一个Criteria
	 * @param session
	 * @param clazz
	 * @return {@link Criteria}
	 */
	public static Criteria createCriteria(Session session, Class clazz){
		return session.createCriteria(clazz);
	}
	
}
